<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adeal extends CI_Model {
    var $table_name = 'adeals';
    var $table_name1 = 'adealimages';
    var $rows_per_page = 20;

    function setRowsPerPage($rows) {
        $this->rows_per_page = $rows;
    }

    function getPageDeals($page){
    	$start = ($page - 1) * $this->rows_per_page;
        $data = $this->db->get($this->table_name, $this->rows_per_page, $start);
        return $data->result();
    }

    function getFirstDeals(){
        $data = $this->db->get($this->table_name, 4, 0);
        return $data->result();
    }

    function getFilteredRows($filter) {
        $data = $this->db->get_where($this->table_name, $filter);
        return $data->result();
    }

    function addADeal($title, $i_cost, $sale, $s_cost, $sale_end, $img_url, $sold, $address,
                      $time, $video, $specs, $cond, $coord, $deal_id, $views, $likes,
                      $city, $tel, $category, $deal_id, $zoom) {
        
        $data = array('title'=>$title,
    		'i_cost'=>$i_cost, 'sale'=>$sale, 's_cost'=>$s_cost, 'sale_end'=>$sale_end, 'img_url'=>$img_url, 'sold'=>$sold, 'address'=>$address,
            'time'=>$time, 'video'=>$video, 'specs'=>$specs, 'cond'=>$cond, 'coord'=>$coord, 'deal_id'=>$deal_id, 'views'=>$views, 'likes'=>$likes,
            'city'=>$city, 'tel'=>$tel, 'category'=>$category, 'deal_id'=>$deal_id, 'zoom' => $zoom
    	);
    	$this->db->insert($this->table_name, $data);
    }

    function editADeal($id, $title, $i_cost, $sale, $s_cost, $sale_end, $img_url, $sold, $address,
                      $time, $video, $specs, $cond, $coord, $deal_id, $views, $likes,
                      $city, $tel, $category, $deal_id, $zoom) {
        $email = strtolower($p_email);
        $data = array('title'=>$title,
            'i_cost'=>$i_cost, 'sale'=>$sale, 's_cost'=>$s_cost, 'sale_end'=>$sale_end, 'img_url'=>$img_url, 'sold'=>$sold, 'address'=>$address,
            'time'=>$time, 'video'=>$video, 'specs'=>$specs, 'cond'=>$cond, 'coord'=>$coord, 'deal_id'=>$deal_id, 'views'=>$views, 'likes'=>$likes,
            'city'=>$city, 'tel'=>$tel, 'category'=>$category, 'deal_id'=>$deal_id, 'zoom' => $zoom
        );
    	$this->db->where('id',$id);
    	$this->db->set($data);
		$this->db->update($this->table_name);
    }

    function editADealNoImage($id, $title, $i_cost, $sale, $s_cost, $sale_end, $sold, $address,
                      $time, $video, $specs, $cond, $coord, $deal_id, $views, $likes,
                      $city, $tel, $category, $deal_id) {
        $email;
        $data = array('title'=>$title,
            'i_cost'=>$i_cost, 'sale'=>$sale, 's_cost'=>$s_cost, 'sale_end'=>$sale_end, 'sold'=>$sold, 'address'=>$address,
            'time'=>$time, 'video'=>$video, 'specs'=>$specs, 'cond'=>$cond, 'coord'=>$coord, 'deal_id'=>$deal_id, 'views'=>$views, 'likes'=>$likes,
            'city'=>$city, 'tel'=>$tel, 'category'=>$category, 'deal_id'=>$deal_id
        );
        $this->db->where('id',$id);
        $this->db->set($data);
        $this->db->update($this->table_name);
    }

    function editColumn($id,$column_name, $value){
        $this->db->where('id', $id);
        $this->db->set($column_name, $value);
        $this->db->update($this->table_name);
    }

    function getRowById($id){
    	$this->db->where('id', $id);
    	$data = $this->db->get($this->table_name);
        return $data->result();
    }

	function belongsToMe(){

	}   
	
	function pageCount(){
		$query = $this->db->get($this->table_name);
		$rowcount = $query->num_rows();
		$pagecount = 0;
		if($rowcount % 20 == 0){
			$pagecount = intval($rowcount / 20);
		}else{
			$pagecount = intval($rowcount / 20) + 1;
		}
		return $pagecount;
	} 

    function getADealImages($id){
        $this->db->where('adeal_id', $id);
        $data = $this->db->get($this->table_name1);
        return $data->result();
    } 

}